package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author zhaoshuo
 * @ClassName IndexController
 * @Description
 */
@Controller
@RequestMapping
public class IndexController {

    @Autowired
    private IndexService indexService;
    @GetMapping({"/", "index", "index.html"})
    public String index(Model model) {
        List<CategoryEntity> categoryEntities = indexService.queryLevelOneCategories();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/cates/{cid}")
    public ResponseVo<List<CategoryEntity>> levelTwoAndSubsCates(@PathVariable("cid")String cid){
        List<CategoryEntity> levelTwoAndSubsCates = indexService.levelTwoAndSubsCates(cid);
        return ResponseVo.ok(levelTwoAndSubsCates);
    }

    @ResponseBody
    @GetMapping("/index/testlock")
    public ResponseVo testLock(){
        try {
            indexService.testLock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ResponseVo.ok();
    }
}